package Views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.GridLayout;


import javax.swing.JLabel;
import javax.swing.JScrollPane;

import Models.Recipe;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import java.awt.Insets;


public class RecipeView extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	


	private RecipePanel currentPanel;  //the panel that holds the current recipe cards
	private RecipePanel parentPnl;

	
	private Recipe currentR;
	private Recipe parentR;
	//TODO dynamic edit components
	
	
	private MainWindow parent;
	

	/**
	 * Create the panel.
	 */
	public RecipeView(MainWindow parent) {
		this.parent = parent;
		
		setBackground(Color.WHITE);
		setLayout(new BorderLayout(0, 0));
		
		createTopPanel();
		createRecipePanels();
		createButtons();
	}
	
	private void createButtons() {
		JPanel pnlButton = new JPanel();
		pnlButton.setBackground(SystemColor.menu);
		add(pnlButton, BorderLayout.SOUTH);
		pnlButton.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnReBrew = new JButton("Re-Brew");
		pnlButton.add(btnReBrew);
		
		JButton btnNew = new JButton("New");
		pnlButton.add(btnNew);
		
		JButton btnSave = new JButton("Save");
		pnlButton.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		pnlButton.add(btnCancel);
		
		JButton btnDelete = new JButton("Delete");
		pnlButton.add(btnDelete);
		btnNew.addActionListener(this);
	}
	
	
	private void createTopPanel() {
		JPanel topPanel = new JPanel();
		add(topPanel, BorderLayout.NORTH);
		GridBagLayout gbl_topPanel = new GridBagLayout();
		gbl_topPanel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_topPanel.rowHeights = new int[]{0, 0};
		gbl_topPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_topPanel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		topPanel.setLayout(gbl_topPanel);
		
		JLabel lblCurrent = new JLabel("Current Recipe");
		GridBagConstraints gbc_lblCurrent = new GridBagConstraints();
		gbc_lblCurrent.weightx = 1.0;
		gbc_lblCurrent.insets = new Insets(0, 0, 0, 5);
		gbc_lblCurrent.gridx = 2;
		gbc_lblCurrent.gridy = 0;
		topPanel.add(lblCurrent, gbc_lblCurrent);
		
		JLabel lblOriginalRecipe = new JLabel("Original Recipe");
		GridBagConstraints gbc_lblOriginalRecipe = new GridBagConstraints();
		gbc_lblOriginalRecipe.insets = new Insets(0, 0, 0, 5);
		gbc_lblOriginalRecipe.weightx = 1.0;
		gbc_lblOriginalRecipe.gridx = 7;
		gbc_lblOriginalRecipe.gridy = 0;
		topPanel.add(lblOriginalRecipe, gbc_lblOriginalRecipe);
	}
	
	private void createRecipePanels() {
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		JPanel viewPanel = new JPanel();
		scrollPane.setViewportView(viewPanel);
		viewPanel.setLayout(new GridLayout(0, 2, 0, 0));
		
		currentPanel = new RecipePanel(RecipePanel.EDIT);  //the left panel
		viewPanel.add(currentPanel);
		
		parentPnl = new RecipePanel(RecipePanel.READ);    //the right panel
		viewPanel.add(parentPnl);
	}
	
	/*
	 * Updates to display the recipes that have been given.
	 */
	public void update(Recipe current, Recipe parent) {
		this.currentR = current;
		this.parentR = parent;
		this.currentPanel.setRecipe(currentR);
		this.parentPnl.setRecipe(parentR);
		
		
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
			//TODO
	}
}
